package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.StatusInfo;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.TaskEntity;
import com.lkd.feign.UserService;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
public class VMStatusMsgHandler implements MsgHandler {
    @Autowired
    private TaskService taskService;

    @Autowired
    private VMService vmService;



    @Override
    public void process(String jsonMsg) throws IOException {



        VmStatusContract vmStatusContract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        List<StatusInfo> statusInfoList = vmStatusContract.getStatusInfo();
        String innerCode = vmStatusContract.getInnerCode();

        //根据机器编号获取货道实体对象
        LambdaQueryWrapper<TaskEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskEntity::getInnerCode, innerCode);
        TaskEntity taskEntity = new TaskEntity();

        for (StatusInfo statusInfo : statusInfoList) {
            if (statusInfo.isStatus() == false){

                TaskViewModel taskViewModel = new TaskViewModel();
                taskViewModel.setCreateType(VMSystem.TASK_TYPE_REPAIR);
                taskViewModel.setInnerCode(innerCode);

                Integer leastUser = taskService.getLeastUser(taskEntity.getRegionId(), VMSystem.USER_REPAIRER);
                taskViewModel.setUserId(leastUser);
                taskViewModel.setAssignorId(0);
                taskViewModel.setProductType(VMSystem.TASK_TYPE_REPAIR);
                taskViewModel.setDesc("维修工单");

                //创建维修工单
                taskService.createTask(taskViewModel);
            }
        }
    }
}
